const App = getApp();

Page({
  data: {
    type: ['预约房间','名厨预约'],
    roomList: [],
    cookList: [],
    current: 0,
    height: 0,
    page: 1,
    lastPage: 0,
    empty: false
  },
  onLoad: function (options) {
    let current = this.data.current;
    if (current == 0){
      this.getRoomList()
    }else{
      this.getCookList()
    }
    this.setSwiperViewHeight()
  },
  //获取预约房间数据
  getRoomList(){
    let _this = this;
    App._get('room/mybooks', {

    }, function (result) {
      if (result.code == 1) {
        let mybooks = result.data.mybooks;
        _this.setData({
          roomList: _this.data.roomList.concat(mybooks.data),
          page: mybooks.current_page,
          lastPage: mybooks.last_page,
          empty: mybooks.data.length > 0 ? false : true
        })
      }
    });
  },
  //获取名厨预约数据
  getCookList() {
    let _this = this;
    let page = this.data.page;

    App._get('cook/mybooks', {
      page: page
    }, function (result) {
      if (result.code == 1) {
        let mybooks = result.data.mybooks;
        _this.setData({
          cookList: _this.data.cookList.concat(mybooks.data),
          page: mybooks.current_page,
          lastPage: mybooks.last_page,
          empty: mybooks.data.length > 0 ? false : true
        })
      }
    });
  },
  // 点击切换视图
  switchView(e){
    let id = e.currentTarget.id;
    let current = id == 0 ? false : true;

    this.setData({
      current,
      roomList: [],
      cookList: []
    })

    if (current == 0) {
      this.getRoomList()
    } else {
      this.getCookList()
    }
  },
  //点击切换菜单||滑动切换
  swiperChange: function (e) {
    let current = e.detail.current;

    if (e.detail.source == 'touch') {
      this.setData({
        current,
        roomList: [],
        cookList: []
      })

      if (current == 0) {
        this.getRoomList()
      } else {
        this.getCookList()
      }
    }
  },
  //设置swiper高度
  setSwiperViewHeight: function () {
    let that = this;
    let h = 240;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.screenHeight * 2 - 200 + 'rpx'
        })
      },
    })
  },
  //下一页
  scrollBottom: function () {
    let page = this.data.page;
    let current = this.data.current;
    
    if (this.data.page < this.data.lastPage) {
      page++;

      this.setData({
        page: page
      })

      if (current == 0) {
        this.getRoomList()
      } else {
        this.getCookList()
      }
    } else {
      wx.showToast({
        title: '亲，没有更多了',
        icon: 'none'
      })
    }
  }
})